var origin = {

    param:{
        originListTemplate:function(){
            return $('#originListTemplate').html();
        }
    },
    url:{
        pageUrl : function(){
            return "/shop/origin/page.json";
        },
        updateUrl : function(isCreate){
            return isCreate ? "/shop/origin/save.json" : "/shop/origin/update.json";
        },
        deleteUrl: function(){
            return "/shop/origin/delete.json";
        }

    },
    //便利List
    loadOriginList :function(){
        var pageSize = $("#pageSize").val();
        var pageNo = $("#originPage .pageNo").val() || 1;
        $.ajax({
            url: origin.url.pageUrl(),
            data: {
                pageNo: pageNo,
                pageSize: pageSize
            },
            type: 'POST',
            success: function (result) {
                console.info("renderOriginListAndPage");
                origin.renderOriginListAndPage(result);
            }
        });
    },
    renderOriginListAndPage : function(result){
        if (result.ret) {
            if (result.data.total > 0) {

                var rendered = Mustache.render(origin.param.originListTemplate(), {
                    "originList": result.data.data,
                    "shopNumber": function () {
                        return function (text, render) {
                            return this.shopNumber;
                        }
                    },
                    "shopName" :function () {
                        return function (text, render) {
                            return this.shopName;
                        }
                    },
                    "shopPrice": function () {
                        return function (text, render) {
                            return this.shopPrice;
                        }
                    },
                    "quantity": function () {
                        return function (text, render) {
                            return this.quantity;
                        }
                    },
                    "amount": function () {
                        return function (text, render) {
                            return this.amount;
                        }
                    },
                    "shopBar": function () {
                        return function (text, render) {
                            return this.shopBar;
                        }
                    },
                    "shopCompany": function () {
                        return function (text, render) {
                            return this.shopCompany;
                        }
                    }
                });
                var originMap = {};

                $('#originList').html(rendered);
                $.each(result.data.data, function (i, origin) {
                    originMap[origin.originId] = origin;
                });
            } else {
                $('#originList').html('');
            }
            origin.bindOriginClick(originMap);
            var pageSize = $("#pageSize").val();
            var pageNo = $("#originPage .pageNo").val() || 1;
            renderPage(origin.url.pageUrl(), result.data.total, pageNo, pageSize,
                result.data.total > 0 ? result.data.data.length : 0, "originPage", origin.renderOriginListAndPage);
        } else {
            showMessage("获取商品列表", result.msg, false);
        }
    },
    //绑定点击事件
    bindOriginClick : function (originMap){
        $(".origin-show").click(function (e) {
            e.preventDefault();
            e.stopPropagation();
            var originId = $(this).attr("data-id");
            $("#dialog-origin-form").dialog({
                model: true,
                title: "商品展示",
                open: function(event, ui) {
                    $(".ui-dialog-titlebar-close", $(this).parent()).hide();
                    $("#originForm")[0].reset();
                    var targetOrigin = originMap[originId];
                    if (targetOrigin) {
                        $("#originId").val(targetOrigin.originId);
                        $("#shopNumber").val(targetOrigin.shopNumber);
                        $("#shopName").val(targetOrigin.shopName);
                        $("#shopPrice").val(targetOrigin.shopPrice);
                        $("#quantity").val(targetOrigin.quantity);
                        $("#amount").val(targetOrigin.amount);
                        $("#shopBar").val(targetOrigin.shopBar);
                        $("#shopCompany").val(targetOrigin.shopCompany);
                    }
                },
                buttons : {
                    "取消": function () {
                        $("#dialog-origin-form").dialog("close");
                    }
                }
            })
        });


        $(".origin-edit").click(function (e) {
            e.preventDefault();
            e.stopPropagation();
            var originId = $(this).attr("data-id");
            $("#dialog-origin-form").dialog({
                model: true,
                title: "修改商品",
                open: function(event, ui) {
                    $(".ui-dialog-titlebar-close", $(this).parent()).hide();
                    $("#originForm")[0].reset();
                    var targetOrigin = originMap[originId];
                    if (targetOrigin) {
                        $("#originId").val(targetOrigin.originId);
                        $("#shopNumber").val(targetOrigin.shopNumber);
                        $("#shopName").val(targetOrigin.shopName);
                        $("#shopPrice").val(targetOrigin.shopPrice);
                        $("#quantity").val(targetOrigin.quantity);
                        $("#amount").val(targetOrigin.amount);
                        $("#shopBar").val(targetOrigin.shopBar);
                        $("#shopCompany").val(targetOrigin.shopCompany);
                    }
                },
                buttons : {
                    "修改": function(e) {
                        e.preventDefault();
                        origin.updateOrigin(false, function (data) {
                            $("#dialog-origin-form").dialog("close");
                            origin.loadOriginList();
                        }, function (data) {
                            showMessage("修改商品", data.msg, false);
                        })
                    },
                    "取消": function () {
                        $("#dialog-origin-form").dialog("close");
                    }
                }
            })
        });

        $(".origin-delete").click(function (e) {
            console.info("delete origin");
            e.preventDefault();
            e.stopPropagation();
            var originId = $(this).attr("data-id");
            var originName = $(this).attr("data-name");

            if (confirm("确定要删除商品[" + originName + "]吗?")) {
                $.ajax({
                    url: origin.url.deleteUrl(),
                    data: {
                        originId: originId
                    },
                    success: function (result) {
                        if (result.ret) {
                            showMessage("删除商品[" + originName + "]", "操作成功", true);
                            origin.loadOriginList();
                        } else {
                            showMessage("删除商品[" + originName + "]", result.msg, false);
                        }
                    }
                });
            }
        });
    },
    //更新或者新增操作
    updateOrigin : function (isCreate, successCallback, failCallback){
        $.ajax({
            url: origin.url.updateUrl(isCreate),
            data: $("#originForm").serializeArray(),
            type: 'POST',
            success: function(result) {
                if (result.ret) {
                    if (successCallback) {
                        successCallback(result);
                    }
                } else {
                    if (failCallback) {
                        failCallback(result);
                    }
                }
            }
        })
    },

    detail:{

        init: function (params){
            var pageSize = params['pageSize'];
            var pageNo = params['pageNo'];

            Mustache.parse(origin.param.originListTemplate());

            origin.loadOriginList();

            $(".research").click(function (e) {
                e.preventDefault();
                origin.loadOriginList();
            });

            $(".origin-add").click(function() {
                $("#dialog-origin-form").dialog({
                    model: true,
                    title: "新增商品",
                    open: function(event, ui) {

                        $("#originForm")[0].reset();
                    },
                    buttons : {
                        "添加": function(e) {
                            e.preventDefault();
                            origin.updateOrigin(true, function (data) {
                                $("#dialog-origin-form").dialog("close");
                                origin.loadOriginList();
                            }, function (data) {
                                showMessage("新增商品", data.msg, false);
                            })
                        },
                        "取消": function () {
                            $("#dialog-origin-form").dialog("close");
                        }
                    }
                });
            });
        }

    }



}